//+------------------------------------------------------------------------------------------------------------------+
#property description                                                                      "[!!!-MT4 X-XARDg-ADX SAR]"
#define Version                                                                                      "[XARDg-ADX SAR]"
//+------------------------------------------------------------------------------------------------------------------+
#property link        "https://forex-station.com/viewtopic.php?p=1295409935#p1295409935"
#property description "THIS IS A FREE INDICATOR"
#property description "                                                      "
#property description "Welcome to the World of Forex"
#property description "Let light shine out of darkness and illuminate your world"
#property description "and with this freedom leave behind your cave of denial"

#property description "Indicator colors the candle according to:"
#property description "- ADX trend given by the DIP and DIM"
#property description "- Close position in regards of the SAR"
#property indicator_chart_window
#property indicator_buffers 8
          string ID="   "; int Type, getChartScale = WRONG_VALUE;
//+------------------------------------------------------------------------------------------------------------------+
     extern bool showCANDLES=false;
      extern int ADX_Periods=55;
   extern double SAR_Step=0.02,SAR_Maximum=0.2;
    extern color Up_Color=C'34,95,174',Dn_Color=C'174,95,34',Wt_Color=C'34,34,34';
          double Up_High[],Up_Low[],Up_Open[],Up_Close[];
          double Dn_High[],Dn_Low[],Dn_Open[],Dn_Close[];
          string WindowName,TimeFrames="1,5,15,30,60,240,1440";
          string TF_arr[],TF_count;
             int WindowNumber,Bullish=1,Bearish=-1,Neutral=0;
//+----OnInit Function-----------------------------------------------------------------------------------------------+
   int OnInit(){WindowName = "ADX SAR";  IndicatorShortName(WindowName);
   if(showCANDLES)        Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
   if(Period()>=PERIOD_H4){showCANDLES=false;}
   SetIndexBuffer(0,Up_High);    SetIndexStyle(0,Type,0,2,Up_Color);
   SetIndexBuffer(1,Up_Low);     SetIndexStyle(1,Type,0,2,Up_Color);
   SetIndexBuffer(2,Up_Open);    SetIndexStyle(2,Type);
   SetIndexBuffer(3,Up_Close);   SetIndexStyle(3,Type);
   SetIndexBuffer(4,Dn_High);    SetIndexStyle(4,Type,0,2,Dn_Color);
   SetIndexBuffer(5,Dn_Low);     SetIndexStyle(5,Type,0,2,Dn_Color);
   SetIndexBuffer(6,Dn_Open);    SetIndexStyle(6,Type);
   SetIndexBuffer(7,Dn_Close);   SetIndexStyle(7,Type);  fSetBuffers(); return(INIT_SUCCEEDED);}//End OnInit
//+----deinit Function-----------------------------------------------------------------------------------------------+
   int deinit(){CleanUpIsle7(); return(0);}//End deinit
//+------------------------------------------------------------------------------------------------------------------+
   int start(){int i,bias,counted_bars=IndicatorCounted();  int limit=Bars-counted_bars-1;
   double dip,dim,sar; fSetBuffers();
   for(i=limit; i>=0; i--){ResetBuffers(i);
      dip = iADX(NULL,0,ADX_Periods,PRICE_CLOSE,MODE_PLUSDI,i);
      dim = iADX(NULL,0,ADX_Periods,PRICE_CLOSE,MODE_MINUSDI,i);
      sar = iSAR(NULL,0,SAR_Step,SAR_Maximum,i);
      bias=ColorCandle(dip, dim, sar, Close[i]);
   if(bias>0){
      Up_High[i]  = iHigh(NULL,0,i);
      Up_Low[i]   = iLow(NULL,0,i);  
      Up_Open[i]  = iOpen(NULL,0,i);
      Up_Close[i] = iClose(NULL,0,i);}  else
   if(bias<0){
      Dn_High[i]  = iHigh(NULL,0,i);
      Dn_Low[i]   = iLow(NULL,0,i);  
      Dn_Open[i]  = iOpen(NULL,0,i);
      Dn_Close[i] = iClose(NULL,0,i);}}
//+------------------------------------------------------------------------------------------------------------------+
   int period;  WindowNumber=WindowFind(WindowName);   
   int Pair_y = 30;  string TF_Label;
   split(TF_arr, TimeFrames, ",");
   TF_count = ArraySize(TF_arr);
   color  diff_color;  string diff_string;
   MakeLbl(ID+"Label",10,4,"ADX SAR",clrGold,1,0,"Arial Bold",15);
   for(i=0;i<TF_count;i++){
   TF_Label = Get_TimeFrame_Label(StringToInteger(TF_arr[i]));
   period = StringToInteger(TF_arr[i]);
      dip = iADX(NULL,period,ADX_Periods,PRICE_CLOSE,MODE_PLUSDI,0);
      dim = iADX(NULL,period,ADX_Periods,PRICE_CLOSE,MODE_MINUSDI,0);
      sar = iSAR(NULL,period,SAR_Step,SAR_Maximum,0);
      bias=ColorCandle(dip,dim,sar,Close[0]);
   if(bias>0){
      diff_color = Up_Color;
      diff_string = "";}  else 
   if(bias<0){
      diff_color = Dn_Color;
      diff_string = "";}  else {
      diff_color = Wt_Color;
      diff_string = "";}
      MakeLbl(ID+TF_Label+"_Label",50,Pair_y,TF_Label,clrWhite,1,0,"Arial",12);
      MakeLbl(ID+Symbol()+"_"+TF_Label,23,Pair_y,diff_string,diff_color,1,0,"Wingdings",12);
      Pair_y = Pair_y+20;}  return(0);}
//+------------------------------------------------------------------------------------------------------------------+
   void ResetBuffers(int shift){
      Up_High[shift]  = EMPTY_VALUE;
      Up_Low[shift]   = EMPTY_VALUE;
      Up_Open[shift]  = EMPTY_VALUE;
      Up_Close[shift] = EMPTY_VALUE;
      Dn_High[shift]  = EMPTY_VALUE;
      Dn_Low[shift]   = EMPTY_VALUE;
      Dn_Open[shift]  = EMPTY_VALUE;
      Dn_Close[shift] = EMPTY_VALUE;  return;}
//+------------------------------------------------------------------------------------------------------------------+
   int ColorCandle(double dip, double dim, double sar, double price){
   if(dip > dim && sar < price)  return(Bullish); else 
   if(dip < dim && sar > price)  return(Bearish); else  return (Neutral);}
//+------------------------------------------------------------------------------------------------------------------+
   void MakeLbl(string nm,int xoff,int yoff,string Txt,color Clr,int Cnr=1,int Win=1,string Font="Arial",int FSize=8){   
    ObjectDelete(nm);
    ObjectCreate(nm,OBJ_LABEL,1,0,0);
       ObjectSet(nm,OBJPROP_CORNER,Cnr);
       ObjectSet(nm,OBJPROP_XDISTANCE,xoff);
       ObjectSet(nm,OBJPROP_YDISTANCE,yoff);
       ObjectSet(nm,OBJPROP_BACK,false);
   ObjectSetText(nm,Txt,FSize,Font,Clr);  return;}
//+------------------------------------------------------------------------------------------------------------------+
   void split(string& arr[],string str,string sym){ArrayResize(arr,0);
   string item;  int pos,size,len=StringLen(str);
   for(int i=0;i<len;){pos=StringFind(str,sym,i);
   if(pos==-1) pos=len;
      item = StringSubstr(str,i,pos-i);
      item = StringTrimLeft(item);
      item = StringTrimRight(item);
      size = ArraySize(arr);
      ArrayResize(arr,size+1);
      arr[size]=item; i=pos+1;}}
//+------------------------------------------------------------------------------------------------------------------+
   string Get_TimeFrame_Label(int TF){string Label;
   if(TF==    1) Label = "M1";
   if(TF==    5) Label = "M5";
   if(TF==   15) Label = "M15";
   if(TF==   30) Label = "M30";
   if(TF==   60) Label = "H1";
   if(TF==  240) Label = "H4";
   if(TF== 1440) Label = "D1";
   if(TF==10080) Label = "W1";
   if(TF==43200) Label = "MN1";  return(Label);}
//+----Clean Chart Function------------------------------------------------------------------------------------------+
   void CleanUpIsle7(){string namem; for(int m=ObjectsTotal()-1; m>=0; m--){namem=ObjectName(m);
   if(StringSubstr(namem,0,StringLen(ID))==ID) {ObjectDelete(namem);}}}//EOF
//+------------------------------------------------------------------------------------------------------------------+
   void fSetBuffers(){int iChartScale = int(ChartGetInteger(0,CHART_SCALE)); if(getChartScale == iChartScale) return;
	getChartScale = iChartScale; int iW=0,rW=0; switch(iChartScale){
		case 0: iW=1; rW=1; break; case 1: iW=1; rW=2; break; case 2: iW= 2; rW= 3; break;
		case 3: iW=4; rW=5; break; case 4: iW=6; rW=9; break; case 5: iW=14; rW=17; break;}
	if(showCANDLES)        Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
	SetIndexStyle(2,Type,0,iW,Up_Color);
   SetIndexStyle(3,Type,0,iW,Up_Color);
   SetIndexStyle(6,Type,0,iW,Dn_Color);
   SetIndexStyle(7,Type,0,iW,Dn_Color); ChartRedraw();}
//+---OnChartEvent Function------------------------------------------------------------------------------------------+
   void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam){
	if(id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON){
   if(StringFind(sparam,ID+":back:"  ,0)==0) ObjectSet(sparam,OBJPROP_STATE,false); 
	if(IsStopped()) return; fSetBuffers();} OnInit();}
//+------------------------------------------------------------------------------------------------------------------+